﻿package com.urbansquall.display {
import flash.display.*;
import flash.geom.Rectangle;

public class Tile9 extends Sprite
{
	internal var m_topLeft : Bitmap;
	internal var m_topMiddle : TiledSection;
	internal var m_topRight : Bitmap;
	
	internal var m_middleLeft : TiledSection;
	internal var m_middleMiddle : TiledSection;
	internal var m_middleRight : TiledSection;
	
	internal var m_bottomLeft : Bitmap;
	internal var m_bottomMiddle : TiledSection;
	internal var m_bottomRight : Bitmap;
	
	internal var m_tile9Data : Tile9Data;
	
	public function Tile9( a_tile9Data : Tile9Data )
	{
		m_tile9Data = a_tile9Data;
		
		m_topLeft = new Bitmap( m_tile9Data.m_topLeft );
		m_topMiddle = new TiledSection( m_tile9Data.m_topMiddle );
		m_topRight = new Bitmap( m_tile9Data.m_topRight );

		m_middleLeft = new TiledSection( m_tile9Data.m_middleLeft );
		m_middleMiddle = new TiledSection( m_tile9Data.m_middleMiddle );
		m_middleRight = new TiledSection( m_tile9Data.m_middleRight );

		m_bottomLeft = new Bitmap( m_tile9Data.m_bottomLeft );
		m_bottomMiddle = new TiledSection( m_tile9Data.m_bottomMiddle );
		m_bottomRight = new Bitmap( m_tile9Data.m_bottomRight );
		
		m_topLeft.smoothing = false;
		m_topRight.smoothing = false;

		m_bottomLeft.smoothing = false;
		m_bottomRight.smoothing = false;		
		
		m_topLeft.pixelSnapping = PixelSnapping.ALWAYS;
		m_topRight.pixelSnapping = PixelSnapping.ALWAYS;

		m_bottomLeft.pixelSnapping = PixelSnapping.ALWAYS;
		m_bottomRight.pixelSnapping = PixelSnapping.ALWAYS;				
				
		addChild( m_topMiddle );
		addChild( m_middleLeft );
		addChild( m_middleMiddle );
		addChild( m_middleRight );
		addChild( m_bottomMiddle );

		// corners go over tiled bits
		addChild( m_topLeft );
		addChild( m_topRight );
		addChild( m_bottomLeft );
		addChild( m_bottomRight );		
		
		reset();
	}
	
	public function reset() : void
	{
		resize( m_topLeft.width + m_topMiddle.width + m_topRight.width, m_topLeft.height + m_middleLeft.height + m_bottomLeft.height );
	}
	
	public function resize( a_width : Number, a_height : Number ) : void
	{
		width = a_width;
		height = a_height;
	}
	
	override public function set width( a_width : Number ) : void
	{
		var middleWidth : Number = Math.ceil( a_width - m_topLeft.width - m_topRight.width );
		
		m_topMiddle.setSize( middleWidth, m_topLeft.height );
		m_middleMiddle.setSize( middleWidth, m_middleMiddle.height );
		m_bottomMiddle.setSize( middleWidth, m_bottomLeft.height );
		
		m_topLeft.x = 0;
		m_topMiddle.x = m_tile9Data.m_tile9Rect.x;
		m_topRight.x = a_width - m_topRight.width;
		
		m_middleLeft.x = 0;
		m_middleMiddle.x = m_topLeft.width;
		m_middleRight.x = m_topLeft.width + m_topMiddle.width;
				
		m_bottomLeft.x = 0;
		m_bottomMiddle.x = m_topLeft.width;
		m_bottomRight.x = a_width - m_bottomRight.width;
	}
	
	override public function set height( a_height : Number ) : void
	{
		var middleHeight : Number = Math.ceil( a_height - m_topLeft.height - m_bottomLeft.height );
		
		m_topMiddle.setSize( m_topMiddle.width, m_topLeft.height );
		m_middleMiddle.setSize( m_middleMiddle.width, middleHeight );
		m_bottomMiddle.setSize( m_bottomMiddle.width, m_bottomLeft.height );
		
		m_middleLeft.setSize( m_topLeft.width, middleHeight );
		m_middleRight.setSize( m_topRight.width, middleHeight );
		
		m_topLeft.y = 0;
		m_topMiddle.y = 0;
		m_topRight.y = 0;
		
		m_middleLeft.y = m_topLeft.height;
		m_middleMiddle.y = m_topLeft.height;
		m_middleRight.y = m_topLeft.height;
		
		m_bottomLeft.y = a_height - m_bottomLeft.height; // m_middleLeft.y + m_middleLeft.height;
		m_bottomMiddle.y = a_height - m_bottomLeft.height;
		m_bottomRight.y = a_height - m_bottomLeft.height;		
	}
} // c
} // p

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.utils.Dictionary;

class TiledSection extends Sprite
{
	private var m_tileBitmapData : BitmapData;
	
	private var m_tiles : Dictionary;
	private var m_mask : Sprite;
	private var m_desiredWidth : int;
	private var m_desiredHeight : int;
	
	function TiledSection( a_tileBitmapData: BitmapData )
	{
		m_tileBitmapData = a_tileBitmapData;
		
		m_tiles = new Dictionary();
	}	
	
	private function clear() : void
	{
		for( var key : String in m_tiles )
		{
			var tile : Bitmap = m_tiles[ key ];
			tile.parent.removeChild( tile );
			delete m_tiles[ key ];
		}
	}
	
	// in pixels, not tiles
	public function setSize( a_width : int, a_height : int ) : void
	{
		m_desiredWidth = a_width;
		m_desiredHeight = a_height;
		
		clear();
		
		draw( a_width, a_height );
		
		if( m_mask != null )
		{
			m_mask.parent.removeChild( m_mask );
			m_mask = null;
		}
		
		m_mask = new Sprite();
		m_mask.graphics.beginFill( 0x00FF00 );

		m_mask.graphics.drawRect( 0, 0, a_width, a_height );
		this.mask = m_mask;
		addChild( m_mask );
	}
	
	private function draw( a_width : int, a_height : int ) : void
	{
		var tilesWide : int = Math.ceil( a_width / m_tileBitmapData.width );
		var tilesHigh : int = Math.ceil( a_height / m_tileBitmapData.height );
		
		tilesWide = Math.max( tilesWide, 1 );
		tilesHigh = Math.max( tilesHigh, 1 );

		trace( tilesWide, tilesHigh );
		
		for( var w : int = 0; w < tilesWide; w++ )
		{
			for( var h : int = 0; h < tilesHigh; h++ )
			{
				var tile : Bitmap = new Bitmap( m_tileBitmapData );
				tile.smoothing = false;
				
				tile.x = w * m_tileBitmapData.width;
				tile.y = h * m_tileBitmapData.height;
				
				m_tiles[ w + "_" + h ] = tile;
				addChild( tile );
			}
		}
	}
	
	override public function get width():Number{ return m_desiredWidth; }
	
	override public function get height():Number { return m_desiredHeight; }
}